/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.util;

import model.pojo.SanPham;
import java.util.ArrayList;
import java.net.*;
import javax.xml.parsers.*;
import model.util.DataHelper;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.*;
import java.util.Properties;

/**
 *
 * @author L_N_S_Binh
 */
public class ControllerHelper 
{
    private static String PathProperties ;
    

    private static String PhanTrang(ArrayList<ArrayList<String>> arr , int vt , String url){
//  {
        String s = "" ;
        String pathImage = "";
        
        s += "<div class='table1'>";
        s += "<div class='row1'>";
        s += "<div align='center' class='cell1'>";
        if(vt + 0 < arr.size())
            s += String.format("<a href='%s'><img src='%s' title='%s' border='0' width='120' height='140'/></a>", url + arr.get(vt + 0).get(0) , pathImage + arr.get(vt + 0).get(3) , arr.get(vt + 0).get(1));
        else 
            s += "<a></a>";
        s += "</div>";

        s += "<div align='center' class='cell2'>";
        if(vt + 1 < arr.size())
            s += String.format("<a href='%s'><img src='%s' title='%s' border='0' width='120' height='140'/></a>", url + arr.get(vt + 1).get(0) , pathImage + arr.get(vt + 1).get(3) , arr.get(vt + 1).get(1));
        else
            s += "<a></a>";
        s += "</div>";

        s += "<div align='center' class='cell3'>";
        if(vt + 2 < arr.size())
            s += String.format("<a href='%s'><img src='%s' title='%s' border='0' width='120' height='140'/></a>", url + arr.get(vt + 2).get(0) , pathImage + arr.get(vt + 2).get(3) , arr.get(vt + 2).get(1));
        else 
            s += "<a></a>";
        s += "</div>";
        s += "</div>";

        s += "<div class='row2'>";
        s += "<div align='center' class='cell1'>";
        if(vt + 0 < arr.size())
            s += String.format("<a href='%s'>%s</a>",url + arr.get(vt + 0).get(0) , arr.get(vt + 0).get(1));
        else 
            s += "<a></a>";
        s += "</div>";

        s += "<div align='center' class='cell2'>";
        if(vt + 1 < arr.size())
            s += String.format("<a href='%s'>%s</a>",url + arr.get(vt + 1).get(0) , arr.get(vt + 1).get(1));
        else 
            s += "<a></a>";
        s += "</div>";

        s += "<div align='center' class='cell3'>";
        if(vt + 2 < arr.size())
            s += String.format("<a href='%s'>%s</a>",url + arr.get(vt + 2).get(0) , arr.get(vt + 2).get(1));
        else 
            s += "<a></a>";
        s += "</div>";
        s += "</div>";

        s += "<div class='row3'>";
        s += "<div align='center' class='cell1'>";
        if(vt + 0 < arr.size())
        {
            s += String.format("<a href='%s' class='left'>%s</a>",url + arr.get(vt + 0).get(0),arr.get(vt + 0).get(2));
            s += String.format("<a href='%s' class='right'>VNĐ</a>",url + arr.get(vt + 0).get(0));
        }
        else 
            s += "<a></a>";
        s += "</div>";

        s += "<div align='center' class='cell2'>";
        if(vt + 1 < arr.size())
        {
            s += String.format("<a href='%s' class='left'>%s</a>",url + arr.get(vt + 1).get(0),arr.get(vt +1).get(2));
            s += String.format("<a href='%s' class='right'>VNĐ</a>",url + arr.get(vt + 1).get(0));  
        }
        else 
            s += "<a></a>";
        s += "</div>";

        s += "<div align='center' class='cell3'>";
        if(vt + 2 < arr.size())
        {
            s += String.format("<a href='%s' class='left'>%s</a>",url + arr.get(vt + 2).get(0),arr.get(vt + 2).get(2));
            s += String.format("<a href='%s' class='right'>VNĐ</a>",url + arr.get(vt + 2).get(0));
        }
        else 
            s += "<a></a>";
        s += "</div>";
        s += "</div>";
        s += "</div>";
        
        return s;
    }
    
   	public static ArrayList<String> PhanTrang(String url , ArrayList<ArrayList<String>> arr){
//  {
        ArrayList<String> html = new ArrayList<String>() ;
        if(arr == null)
        {
        	html.add("");
        	return html;
        }
        for(int i = 0 ; i < arr.size() ; i = i + 6)
        {
        	String temp = "" ;
        	temp += PhanTrang(arr , i + 0 , url);				
        	temp += "<div style='border-top:3px solid #757575; margin:10px 5px;'></div>\n";
        	temp += PhanTrang(arr , i + 3 , url);
        	html.add(temp);
        }
        return html;
    }
//    
    private static FileInputStream openFileToRead(String fileName){
//  {        
        try 
        {
            return new FileInputStream(fileName);
        }
        catch (FileNotFoundException ex) 
        {
            return null ;
            //Logger.getLogger(ControllerHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private static boolean closeFile(FileInputStream is){
//  {    
        try 
        {
            is.close();
            return true ;
        } 
        catch (IOException ex) 
        {
            return false ;
            //Logger.getLogger(ControllerHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static String getStoreProcedureString(String key){
//  {
        try 
        {
            
            FileInputStream fis = openFileToRead(PathProperties);
            if(fis == null)
                return null ;
            Properties prop = new Properties();
            prop.load(fis);

            String str = prop.getProperty(key);
            closeFile(fis);
            return str ;
        } catch (IOException ex) {
            return null ;//Logger.getLogger(ControllerHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static Element layNodeGocXML(String xmlPath){
//  {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //DocumentBuilder builder = factory.newDocumentBuilder();
        try
        {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new File(xmlPath));
            return doc.getDocumentElement();
        }
        catch (SAXException ex) 
        {
            //Logger.getLogger(ControllerHelper.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (IOException ex) 
        {
            //Logger.getLogger(ControllerHelper.class.getName()).log(Level.SEVERE, null, ex);
        }        
        catch(ParserConfigurationException ex)
        {
            //
        }
        return null;        
    }
    
    public static boolean napThongTinKetNoi(String xmlPath , String propertiesPath){
//  {
        Element root = layNodeGocXML(xmlPath); 
        if(root == null)
            return false;
        NodeList list = root.getChildNodes();
        
        DataHelper.setConnectionString(list.item(1).getTextContent());
        for (int i = 0; i < list.getLength(); ++i) 
        {
            Node node = list.item(i);
            if(node instanceof Element) 
            { 
              Element elm = (Element) node;
              
              if(elm.getTagName().equals("String"))
                  DataHelper.setConnectionString(elm.getTextContent());
              
              if(elm.getTagName().equals("User"))
                  DataHelper.setUser(elm.getTextContent());
              
              if(elm.getTagName().equals("PassWord"))
                  DataHelper.setPass(elm.getTextContent());
            }
        }
        
        PathProperties = propertiesPath ;
        return true ;
    }
    public static String filter(String input)
    {
    	//String kq = new String(input.replace("\\","\\\"));
    	//kq = new String (kq.replace("'", "\'"));
    	//kq = new String(kq.replace("\"", '"');
    	return input;
//    	StringBuilder filtered = new StringBuilder(input.length());
//    	char c;
//    	for(int i=0;i<input.length();i++)
//    	{
//    		c= input.charAt(i);
//    		switch(c)
//    		{
//    			case '#': filtered.append("&#35;");break;
//    			case '%': filtered.append("&#37;");break;
//    			case '!': filtered.append("&#33;");break;
//    			case '"': filtered.append("&#34;");break;
//    			case '$': filtered.append("&#36;");break;
//    			case '&': filtered.append("&#38;");break;
//    			case '(': filtered.append("&#40;");break;
//    			case ')': filtered.append("&#41;");break;
//    			case '*': filtered.append("&#42;");break;
//    			case '+': filtered.append("&#43;");break;
//    			case '-': filtered.append("&#45;");break;
//    			case '.': filtered.append("&#46;");break;
//    			case '/': filtered.append("&#47;");break;
//    			case ':': filtered.append("&#58;");break;
//    			case ';': filtered.append("&#59;");break;
//    			case '<': filtered.append("&#60;");break;
//    			case '=': filtered.append("&#61;");break;
//    			case '>': filtered.append("&#62;");break;
//    			case '?': filtered.append("&#63;");break;
//    			case '@': filtered.append("&#64;");break;
//    			case ']': filtered.append("&#93;");break;
//    			case '^': filtered.append("&#94;");break;
//    			case '_': filtered.append("&#95;");break;
//    			case '`': filtered.append("&#96;");break;
//    			case '{': filtered.append("&#123;");break;
//    			case '|': filtered.append("&#124;");break;
//    			case '}': filtered.append("&#125;");break;
//    			case '~': filtered.append("&#126;");break;
//    			case '[': filtered.append("&#91;");break;
//    			default:  filtered.append(c);
//    		}
//    	}
//    	return filtered.toString();
    	//return input;
    }
    
}

